<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Auths;
use app\admin\model\Builder;
use think\Controller;
use think\Db;
use think\Request;

class Lianbiao extends Controller
{
    public function show_list(Request $request){
        $modelid = $request->param('modelid',null);
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        $model   = Db::name('model')->where('id',$modelid)->find();
        $page    = $request->param('page',null);
        $limit   = $request->param('limit',10);
        if(!$page){
            $data = Db::name($model['surface'])->paginate($limit)->toArray();
            $this->assign('data',$data['total']);
            $data = $data['data'];
        }else{
            $data = Db::name($model['surface'])->limit(($page-1)*$limit,$limit)->select();
        }

        $this->assign('modelid',$modelid);
        foreach ($data as $key=>$value){
            if($value['pid']==0){
                $data[$key]['pid_name'] = '最上级';
            }else{
                $data[$key]['pid_name'] = (Db::name($model['surface'])->where('id',$value['pid'])->find())['name'];
            }
        }
        if($page){
            return json(['code'=>200,'data'=>$data]);
        }
        $str = Builder::make('table','','','','','')
            ->addHead(['button alldel','button add'],['批量删除','添加'],['/admin/lianbiao/del?modelid='.$modelid,'/admin/lianbiao/show_add?model_id='.$modelid],['',''],['',''])
            ->addColumn('INDEX','checkbox','<input type="checkbox"/>','','text-align: center')
            ->addColumn('id','text','ID','','text-align: center')
            ->addColumn('name','text','名称','','text-align: center')
            ->addColumn('pid_name','text','上级','','text-align: center')
            ->addRight('操作',['icon edit','icon del'],['/admin/lianbiao/show_edit?modelid='.$modelid.'&id=','/admin/lianbiao/del?modelid='.$modelid],['编辑','删除'],'','text-align: center')
            ->addData($data)
//        echo '<pre>';print_r($str);exit;
            ->addPage('test')
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('list');
    }
    public function show_add(Request $request){
        $modelid = $request->param('model_id',null);
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
//        return $modleid;
        $model = Db::name('model')->where('id',$modelid)->find();
        $feild = Db::name('table_field')->where('modelid',$modelid)->select();
        $pid   = Db::name($model['surface'])->select();
        $option[0] = '最上级';
        foreach ($pid as $value){
            $option[$value['id']] = $value['name'];
        }
//        echo '<pre>';print_r($feild);exit;
        $str = Builder::make('form','/admin/lianbiao/add?modelid='.$modelid,'','');
        foreach ($feild as $key=>$v){
            if($v['field_type']=='单行文本'){
                if($v['field_title']=='pid'){
                    $str = $str->addSelect($v['field_title'],$v['field_name'],'',$option);
                }else{
                    $str = $str->addInput($v['field_title'],$v['field_name'],'','','width:50%');
                }
            }elseif ($v['field_type']=='多行文本'){
                $str = $str->addTextarea($v['field_title'],$v['field_name'],'','','width:50%');
            }elseif ($v['field_type']=='单图片'){
                $str = $str->addImg($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='多图片'){
                $str = $str->addImgs($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='单文件'){
                $str = $str->addFile($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='多文件'){
                $str = $str->addFiles($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='单选框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,;，；\r\n]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addRadio($v['field_title'],$v['field_name'],'',$v['type_option'],'','',true);
            }elseif ($v['field_type']=='复选框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,;，；\r\n]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addCheckbox($v['field_title'],$v['field_name'],'',$v['type_option'],'','',true);
            }elseif ($v['field_type']=='下拉框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,;，；\r\n]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addSelect($v['field_title'],$v['field_name'],'',$v['type_option'],'','',true);
            }elseif ($v['field_type']=='编辑文本'){
                $str = $str->addEditor($v['field_title'],$v['field_name'],'');
            }
        }
        $str = $str->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }

    /**添加联表
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request){
        $data = $request->post();
        $modelid = $request->param('modelid');
        $model = Db::name('model')->where('id',$modelid)->find();
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        if(Db::name($model['surface'])->insert($data)){
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/lianbiao/show_list?modelid='.$modelid]);
        }else{
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/lianbiao/show_list?modelid='.$modelid]);
        }
    }
    public function show_edit(Request $request){
        $modelid = $request->param('modelid');
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        $id = $request->param('id');
        $model = Db::name('model')->where('id',$modelid)->find();
        $data = Db::name($model['surface'])->where('id',$id)->find();
        $feild = Db::name('table_field')->where('modelid',$modelid)->select();
        $pid   = Db::name($model['surface'])->select();
        $option[0] = '最上级';
        foreach ($pid as $value){
            if(!($value['id']==$id)){
                $option[$value['id']] = $value['name'];
            }
        }
//        return 1;
        $str = Builder::make('form','/admin/lianbiao/update?modelid='.$modelid,'','');
        $str = $str->addNone('id',$id);
        foreach ($feild as $key=>$v){
            if($v['field_type']=='单行文本'){
                if($v['field_title']=='pid'){
                    $str = $str->addSelect($v['field_title'],$v['field_name'],$data['pid'],$option);
                }else{
                    $str = $str->addInput($v['field_title'],$v['field_name'],$data[$v['field_title']],'','width:50%');
                }
            }elseif ($v['field_type']=='多行文本'){
                $str = $str->addTextarea($v['field_title'],$v['field_name'],$data[$v['field_title']],'','width:50%');
            }elseif ($v['field_type']=='单图片'){
                $str = $str->addImg($v['field_title'],$v['field_name'],$data[$v['field_title']]);
            }elseif ($v['field_type']=='多图片'){
                $str = $str->addImgs($v['field_title'],$v['field_name'],$data[$v['field_title']]);
            }elseif ($v['field_type']=='单文件'){
                $str = $str->addFile($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='多文件'){
                $str = $str->addFiles($v['field_title'],$v['field_name']);
            }elseif ($v['field_type']=='单选框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,;，；\r\n]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addRadio($v['field_title'],$v['field_name'],'',$v['type_option'],$data[$v['field_title']],'',true);
            }elseif ($v['field_type']=='复选框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,;，；\r\n]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addCheckbox($v['field_title'],$v['field_name'],'',$v['type_option'],$data[$v['field_title']],'',true);
            }elseif ($v['field_type']=='下拉框'){
                if($v['type_option']){
                    $v['type_option'] = preg_split('/[,;，；\r\n]+/', trim($v['type_option'], ",;\r\n"));
                }
                $str = $str->addSelect($v['field_title'],$v['field_name'],'',$v['type_option'],$data[$v['field_title']],'',true);
            }elseif ($v['field_type']=='编辑文本'){
                $str = $str->addEditor($v['field_title'],$v['field_name'],$data[$v['field_title']]);
            }
        }
        $str = $str->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }
    public function update(Request $request){
        $modelid= $request->param('modelid');
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        $data = $request->post();
        $model = Db::name('model')->where('id',$modelid)->find();
        if(Db::name($model['surface'])->where('id',$data['id'])->update($data)){
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/lianbiao/show_list?modelid='.$modelid]);
        }else{
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/lianbiao/show_list?modelid='.$modelid]);
        }
    }
    public function del(Request $request){
        $modelid = $request->param('modelid');
        if(!Auths::login_auth($modelid)){
            return json(['code'=>203,'msg'=>'你没有权限']);
        }
        $id      = $request->param('id');
        $model = Db::name('model')->where('id',$modelid)->find();
        if(Db::name($model['surface'])->where('id','in',$id)->delete()){
                return json(['code'=>200,'msg'=>'删除成功']);
        }else{
                return json(['code'=>203,'msg'=>'删除失败']);
        }
    }
}